package com.global.college;


import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;

import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;

import com.gauravk.bubblenavigation.BubbleNavigationLinearView;
import com.gauravk.bubblenavigation.listener.BubbleNavigationChangeListener;
import com.global.college.adapter.ScreenSlidePagerAdapter;
import com.global.college.ui.fragment.home.HomeSelectFragment;
import com.global.college.ui.fragment.lesson.HomeCourseXFragment;
import com.global.college.utils.GooglePlayUtil;
import com.gyf.barlibrary.ImmersionBar;
import com.global.college.base.BaseActivity;
import com.global.college.ui.bean.UploadHeaderModel;
import com.global.college.ui.bean.UserDataModel;
import com.global.college.ui.fragment.lesson.HomeCourseFragment;
import com.global.college.ui.fragment.mine.MineFragment;
import com.global.college.ui.interfaces.IUserInfoView;
import com.global.college.ui.presenter.UserInfoPresenter;
import com.global.college.utils.AppManagerUtils;

import java.util.ArrayList;

import androidx.viewpager.widget.ViewPager;


public class MainActivity extends BaseActivity implements IUserInfoView {


    private ConstraintLayout con_layout;
    private BubbleNavigationLinearView bubbleNavigationLinearView;
    private ViewPager viewPager;

    private ArrayList<Fragment> fragments;
    private HomeCourseXFragment homeCourseFragment;
    private MineFragment mineFragment;
    private static final String HOME_KEY = "homePageFragment";
    private static final String MALL_KEY = "homeMallFragment";
    private static final String MINE_KEY = "homeMineFragment";
    private ScreenSlidePagerAdapter pagerAdapter;

    public String trainCover;

    private UserInfoPresenter userInfoPresenter;
    /**
     * 定义是否已退出应用
     */
    private static boolean isExit = false;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            isExit = false;
        }
    };

    private HomeSelectFragment homeSelectFragment;

    @Override
    public int getContentViewId() {
        return R.layout.activity_bottom_bar;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        useImmersionBar = false;
//        setTheme(com.aliyun.vodplayer.R.style.NoActionTheme);//隐藏状态栏
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onCreateNew(Bundle savedInstanceState) {
        //初始化Oss用于上传
        setShowTitleBar(false);
        userInfoPresenter = new UserInfoPresenter(this);
        userInfoPresenter.setUserInfoViewView(this);
        userInfoPresenter.getUserInfo();
        fragments = new ArrayList<Fragment>();

        try {
            if (savedInstanceState != null) {
                /*获取保存的fragment  没有的话返回null*/
                homeSelectFragment = (HomeSelectFragment) getSupportFragmentManager().getFragment(savedInstanceState, HOME_KEY);
                homeCourseFragment = (HomeCourseXFragment) getSupportFragmentManager().getFragment(savedInstanceState, MALL_KEY);
                mineFragment = (MineFragment) getSupportFragmentManager().getFragment(savedInstanceState, MINE_KEY);


                addToList(homeSelectFragment);
                addToList(homeCourseFragment);
                addToList(mineFragment);
            } else {
                homeSelectFragment = new HomeSelectFragment();
                homeCourseFragment = new HomeCourseXFragment();
                mineFragment = new MineFragment();
                fragments.add(homeSelectFragment);
                fragments.add(homeCourseFragment);
                fragments.add(mineFragment);
            }
            pagerAdapter = new ScreenSlidePagerAdapter(fragments, getSupportFragmentManager());
        } catch (Exception e) {
            e.printStackTrace();
        }

        bubbleNavigationLinearView = findViewById(R.id.bottom_navigation_view_linear);
        con_layout = findViewById(R.id.con_layout);

        bubbleNavigationLinearView.setBadgeValue(0, null);
        bubbleNavigationLinearView.setBadgeValue(1, null); //invisible badge
        bubbleNavigationLinearView.setBadgeValue(2, null);

        viewPager = findViewById(R.id.view_pager);
        viewPager.setOffscreenPageLimit(3);
        viewPager.setAdapter(pagerAdapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {
            }

            @Override
            public void onPageSelected(int i) {
                bubbleNavigationLinearView.setCurrentActiveItem(i);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        initListener();

        GooglePlayUtil.getInstance().init(this);
    }

    protected void initListener() {
        bubbleNavigationLinearView.setNavigationChangeListener(new BubbleNavigationChangeListener() {
            @Override
            public void onNavigationChanged(View view, int position) {
                viewPager.setCurrentItem(position, true);
                if (position == 2) {
                    con_layout.setBackgroundResource(R.drawable.bg_mine_mian);
                } else if (position == 1) {
                    con_layout.setBackgroundResource(R.drawable.bg_fragment_lesson);
                }
            }
        });
    }


    @Override
    public void getInitData() {

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {// 返回事件
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void exit() {// 双击返回键退出应用
        if (!isExit) {
            isExit = true;
            Toast.makeText(this, getString(R.string.return_desktop_tip), Toast.LENGTH_SHORT).show();
            // 返回键双击延迟
            mHandler.sendEmptyMessageDelayed(0, 2000);
        } else {
            finish();
        }
    }


    private void addToList(Fragment fragment) {
        if (fragment != null) {
            fragments.add(fragment);
        }
    }

    public void jumpTrain() {
        homeSelectFragment.jumpTrain();
    }

    /**
     * 跳转课程页
     */
    public void jump2Course() {
//        tabAdapter.setCurrentFragment(1);
        viewPager.setCurrentItem(1, true);
    }

    public void jump1Home() {
//        tabAdapter.setCurrentFragment(1);
        viewPager.setCurrentItem(0, true);
    }

    public void setCourseCover(String url) {
        trainCover = url;
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }


    @Override
    public void onGetUserInfoSuccess(UserDataModel userDataModel) {
        UserData.getInstance().setPhone(userDataModel.getUserName());
        UserData.getInstance().setUserModel(userDataModel);
        homeSelectFragment.setUserInfo(userDataModel);
    }

    @Override
    public void onGetUserInfoFail(String error) {
    }

    @Override
    public void onSetUserInfoSuccess(Object smsModel) {

    }

    @Override
    public void onSetUserInfoFail(String error) {
    }

    @Override
    public void onUploadHeaderSuccess(UploadHeaderModel smsModel) {

    }

    @Override
    public void onUploadHeaderFail(String error) {

    }
}


///**
// * 首页
// */
//public class MainActivity extends BaseActivity implements IUserInfoView {
//    @BindView(R.id.iv_tab_home_page)
//    ImageView ivTabHomePage;
//    @BindView(R.id.tv_tab_home_page)
//    TextView tvTabHomePage;
//    @BindView(R.id.iv_tab_home_mall)
//    ImageView ivTabHomeClass;
//    @BindView(R.id.tv_tab_home_mall)
//    TextView tvTabHomeClass;
//    @BindView(R.id.iv_tab_home_mine)
//    ImageView ivTabHomeMine;
//    @BindView(R.id.tv_tab_home_mine)
//    TextView tvTabHomeMine;
//    private ArrayList<Fragment> fragments;
//    private FragmentTabAdapter tabAdapter;
//
//    private static final String HOME_KEY = "homePageFragment";
//    private static final String MALL_KEY = "homeMallFragment";
//    private static final String MINE_KEY = "homeMineFragment";
//    private UserInfoPresenter userInfoPresenter;
//    /**
//     * 定义是否已退出应用
//     */
//    private static boolean isExit = false;
//    private Handler mHandler = new Handler() {
//        @Override
//        public void handleMessage(Message msg) {
//            isExit = false;
//        }
//    };
//
//    private HomeFragment homeFragment;
//    private HomeCourseFragment homeCourseFragment;
//    private MineFragment mineFragment;
//
//    public String trainCover;
//
//    @Override
//    public int getContentViewId() {
//        return R.layout.activity_main;
//    }
//
//    @Override
//    public void onCreateNew(Bundle savedInstanceState) {
//        //初始化Oss用于上传
//        setShowTitleBar(false);
//        ImmersionBar.with(this)
//                .statusBarDarkFont(true)
//                .init();//.hideBar(BarHide.FLAG_HIDE_STATUS_BAR)
//        userInfoPresenter = new UserInfoPresenter(this);
//        userInfoPresenter.setUserInfoViewView(this);
//        userInfoPresenter.getUserInfo();
//        fragments = new ArrayList<>();
//        try {
//            if (savedInstanceState != null) {
//                /*获取保存的fragment  没有的话返回null*/
//                homeFragment = (HomeFragment) getSupportFragmentManager().getFragment(savedInstanceState, HOME_KEY);
//                homeCourseFragment = (HomeCourseFragment) getSupportFragmentManager().getFragment(savedInstanceState, MALL_KEY);
//                mineFragment = (MineFragment) getSupportFragmentManager().getFragment(savedInstanceState, MINE_KEY);
//
//
//                addToList(homeFragment);
//                addToList(homeCourseFragment);
//                addToList(mineFragment);
//            } else {
//                homeFragment = new HomeFragment();
//                homeCourseFragment = new HomeCourseFragment();
//                mineFragment = new MineFragment();
//                fragments.add(homeFragment);
//                fragments.add(homeCourseFragment);
//                fragments.add(mineFragment);
//            }
//            tabAdapter = new FragmentTabAdapter(MainActivity.this, fragments, R.id.fl_layout);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        initListener();
//    }
//
//    public void jumpTrain(){
//        homeFragment.jumpTrain();
//    }
//
//    public void setCourseCover(String url){
//        trainCover = url;
//    }
//
//    private void addToList(Fragment fragment) {
//        if (fragment != null) {
//            fragments.add(fragment);
//        }
//    }
//
//    @Override
//    protected void onSaveInstanceState(Bundle outState) {
//        super.onSaveInstanceState(outState);
//    }
//
//    @Override
//    public void getInitData() {
//    }
//
//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {// 返回事件
//        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            exit();
//            return false;
//        }
//        return super.onKeyDown(keyCode, event);
//    }
//
//    private void exit() {// 双击返回键退出应用
//        if (!isExit) {
//            isExit = true;
//            Toast.makeText(this, getString(R.string.return_desktop_tip), Toast.LENGTH_SHORT).show();
//            // 返回键双击延迟
//            mHandler.sendEmptyMessageDelayed(0, 2000);
//        } else {
//            AppManagerUtils.getInstance().finishSpalshActivity();
//            finish();
//        }
//    }
//
//    protected void initListener() {
//        tabAdapter.setOnTabChangeListener(new FragmentTabAdapter.OnTabChangeListener() {
//            @Override
//            public void OnTabChanged(int index) {
//                tvTabHomePage.setTextColor(getResources().getColor(R.color.color_79797A));
//                tvTabHomeClass.setTextColor(getResources().getColor(R.color.color_79797A));
//                tvTabHomeMine.setTextColor(getResources().getColor(R.color.color_79797A));
//
//                ivTabHomePage.setImageResource(R.mipmap.main_home_unselect);
//                ivTabHomeClass.setImageResource(R.mipmap.main_class_unselect);
//                ivTabHomeMine.setImageResource(R.mipmap.main_mine_unselect);
//
//                switch (index) {
//                    case 0:
//                        tvTabHomePage.setTextColor(getResources().getColor(R.color.color_red));
//                        ivTabHomePage.setImageResource(R.mipmap.main_home_selected);
//                        break;
//                    case 1:
//                        tvTabHomeClass.setTextColor(getResources().getColor(R.color.color_red));
//                        ivTabHomeClass.setImageResource(R.mipmap.main_class_selected);
//                        break;
//                    case 2:
//                        tvTabHomeMine.setTextColor(getResources().getColor(R.color.color_red));
//                        ivTabHomeMine.setImageResource(R.mipmap.main_mine_selected);
//                        break;
//                    default:
//                        break;
//                }
//
//            }
//        });
//
//    }
//
//    @OnClick({R.id.ll_tab_home, R.id.ll_tab_lesson, R.id.ll_tab_mine})
//    public void onViewClicked(View view) {
//        switch (view.getId()) {
//            case R.id.ll_tab_home:
//                ImmersionBar.with(this)
//                        .statusBarColor(R.color.white)
//                        .statusBarDarkFont(true)
//                        .init();
//                tabAdapter.setCurrentFragment(0);
//                break;
//            case R.id.ll_tab_lesson:
//                ImmersionBar.with(this)
//                        .statusBarColor(R.color.white)
//                        .statusBarDarkFont(true)
//                        .init();
//                tabAdapter.setCurrentFragment(1);
//                break;
//            case R.id.ll_tab_mine:
//                ImmersionBar.with(this)
//                        .statusBarColor(R.color.color_ED1428)
//                        .statusBarDarkFont(false)
//                        .init();
//                tabAdapter.setCurrentFragment(2);
//                break;
//            default:
//                break;
//        }
//    }
//
//    /**
//     * 跳转课程页
//     */
//    public void jump2Course(){
//        tabAdapter.setCurrentFragment(1);
//    }
//
//    @Override
//    protected void onResume() {
//        super.onResume();
//    }
//
//    @Override
//    public void onDestroy() {
//        super.onDestroy();
//    }
//
//    @Override
//    public void onGetUserInfoSuccess(UserDataModel userDataModel) {
//        UserData.getInstance().setPhone(userDataModel.getUserName());
//        UserData.getInstance().setUserModel(userDataModel);
//    }
//
//    @Override
//    public void onGetUserInfoFail(String error) {
//    }
//
//    @Override
//    public void onSetUserInfoSuccess(Object smsModel) {
//
//    }
//
//    @Override
//    public void onSetUserInfoFail(String error) {
//    }
//
//    @Override
//    public void onUploadHeaderSuccess(UploadHeaderModel smsModel) {
//
//    }
//
//    @Override
//    public void onUploadHeaderFail(String error) {
//
//    }
//}